#!/bin/bash
new_pass=Aa123456.
yum localinstall mysql80-community-release-el9-1.noarch.rpm -y
yum install mysql-community-server --nogpgcheck -y
yum install expect redis -y
pass=$(grep password /var/log/mysqld.log|awk -F '{print $NF}')
x_pass(){
/usr/bin/expect <<EOF > /dev/null 2>&1
  spawn mysql_secure_installation
  expect {
  "Enter" { send "$pass\r"; exp_continue }
  "New" { send "$new_pass\r"; exp_continue }
  "Re-enter new password:" { send "$new_pass\r"; exp_continue }
  "Change" { send "no\r"; exp_continue }
  "anon" { send "y\r"; exp_continue }
  "Disallow" { send "n\r"; exp_continue }
  "test" { send "y\r"; exp_continue }
  "Reload" { send "y\r" }


}
expect eof
EOF
}

flush(){
  mysql -uroot -p$new_pass -e "CREATE USER 'root'@'%' IDENTIFIED BY 'Aa123456.'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;"

}
systemctl restart mysqld
systemctl enable mysqld
x_pass
flush
mysql -uroot -pAa123456. < ry-zzyl_20240913.sql
sed -i 's/^bind.*$/bind * -::*/' /etc/redis/redis.conf
sed -i 's/^.*requirepass.*$/requirepass 123456' /etc/redis/redis.conf
systemctl restart redis
systemctl enable redis

ssh  root@192.168.88.128 <<EOF
mkdir /usr/local/java
mv /opt/openjdk-11.0.0.2_linux-x64.tar.gz /usr/local/java
cd /usr/local/java
tar -zxf openjdk-11.0.0.2_linux-x64.tar.gz
echo 'export JAVA_HOME=/usr/local/java/jdk-11.0.0.2' >> /etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
echo 'export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib' >> /etc/profile
source /etc/profile
mkdir /usr/local/download
mv /opt/apache-tomcat-9.0.97.tar.gz /usr/local/download
cd /usr/local/download
tar -zxf apache-tomcat-9.0.97.tar.gz
mv /opt/zzyl-admin.war ./*tomcat*/webapps
bash *tomcat*/bin/startup.sh
bash *tomcat*/bin/shutdown.sh
bash *tomcat*/bin/startup.sh
EOF

ssh root@192.168.88.130 <<EOF
yum install nginx -y
mkdir /var/www
mv /opt/dist /var/www
sed -i '/include /etc/nginx/conf.d/*.conf;/a add_header Access-Control-Allow-Origin *;\
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";\
        add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization";' /etc/nginx/nginx.conf
sed -i '46a \
        location / {\
            root   /var/www/dist;\
            index  index.html index.htm;\
            proxy_set_header   Upgrade          $http_upgrade;\
            proxy_set_header   Connection       upgrade;\
            try_files $uri $uri/ /index.html;\
        }\
        location /prod-api/ {\
            proxy_pass http:\/\/192.168.88.128:8080\/zzyl-admin\/;\
            proxy_set_header   Upgrade          $http_upgrade;\
            proxy_set_header   Connection       upgrade;\
        }' /etc/nginx/nginx.conf
nginx -s reload
systemctl restart nginx
EOF
